package visual;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import main.GameDef;
import main.GameMain;
import net.GameClient;
import net.Message;
import net.UserServer;

public class FrameHandler implements GameDef{
	public static Frame frame;
	public static boolean hasToPutPiece = true;
	public static boolean hasToPutCastle = true;
	public static boolean hasToPutPieceVertical = false;
	public static int hasToPutPieceId = -1;
	public static Vector <String> piecesNames;
	public static void addFrame(Frame _frame){
		frame = _frame;
		piecesNames = new Vector<String>(STRING_PIECES.length);
		for(int i = 0; i < STRING_PIECES.length; i++){
			piecesNames.add(STRING_PIECES[i]);
		}
	}
	
	public static void handleActionEvent(ActionEvent event, Vector attributes) {
		System.out.println("event: " + event);
		if(event.getActionCommand().equals(MENU_GAME_STRINGS[MENU_GAME_STRING_NEWGAME])){
			createNewGameDialog();
		}else if(event.getActionCommand().equals(MENU_GAME_STRINGS[MENU_GAME_STRING_EXITGAME])){
			frame.close();
		}else if(event.getActionCommand().equals(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_STARTBUTTON])){
			frame.createGameBoard();
			if(attributes.get(2).equals(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_SERVER])){
				GameMain.client = new GameClient("localhost");
				GameMain.server = new UserServer();
			} else {
				GameMain.client = new GameClient((String)attributes.get(0));
			}
		}else if(event.getActionCommand().equals(DIALOG_BUY_STRINGS[DIALOG_BUY_BUY])){
			showBuyDialog();
		}else if(event.getActionCommand().equals(DIALOG_BUY_STRINGS[DIALOG_BUY_PAY])){
			GameMain.client.sendMessage(new Message(DIALOG_BUY_STRINGS[DIALOG_BUY_PAY], attributes));
			hasToPutPiece = true;
			hasToPutPieceVertical = attributes.get(1).equals(DIALOG_BUY_STRINGS[DIALOG_BUY_VERTICAL]);
			hasToPutPieceId = piecesNames.indexOf(attributes.get(0));
		}else if(event.getActionCommand().equals(DIALOG_ATTACK_STRINGS[DIALOG_ATTACK_ATTACK])){
			showAttackDialog();
		}
	}
	
	public static void createNewGameDialog(){
		Dialog newGameFrame = new Dialog(frame, DIALOG_NEWGAME_STRINGS[DIALOG_FRAME_NAME]){ //Clase anonima de InternalFrame
			JTextField ip = new JTextField(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_IP]);
			ButtonGroup group = new ButtonGroup();
			JTextField playerName = new JTextField(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_ENTERNAME]);
			
			public void actionPerformed(ActionEvent event) {
				if(event.getActionCommand().equals(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_CLIENT])){
					ip.setVisible(true);
				} else if(event.getActionCommand().equals(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_SERVER])){
					ip.setText(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_IP]);
					ip.setVisible(false);
				} else if(event.getActionCommand().equals(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_STARTBUTTON])){
					attributes.add(ip.getText());
					attributes.add(playerName.getText());
					attributes.add(group.getSelection().getActionCommand());
					dispose();
				}
				handleActionEvent(event, attributes);
			}
			
			public void createDialog(){
				playerName = new JTextField(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_ENTERNAME]);
				playerName.setBorder(BorderFactory.createTitledBorder(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_PLAYER]));
				getContentPane().setLayout(new FlowLayout());
				getContentPane().add(playerName);
				
				JRadioButton clientButton = new JRadioButton(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_CLIENT]);
				clientButton.setActionCommand(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_CLIENT]);
			    JRadioButton serverButton = new JRadioButton(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_SERVER]);
			    serverButton.setActionCommand(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_SERVER]);
			    group = new ButtonGroup();
			    group.add(clientButton);
			    group.add(serverButton);
			    clientButton.addActionListener(this);
			    serverButton.addActionListener(this);
		        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
		        radioPanel.add(clientButton);
		        radioPanel.add(serverButton);
		        getContentPane().add(radioPanel);
		        
				ip = new JTextField(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_IP]);
				ip.setBorder(BorderFactory.createTitledBorder(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_CONNECTTO]));
				getContentPane().add(ip);

		        JButton startGame = new JButton(DIALOG_NEWGAME_STRINGS[DIALOG_NEWGAME_STARTBUTTON]);
				startGame.addActionListener(this);
				getContentPane().add(startGame);
			}
		};
	}
	
	public static void showBuyDialog(){
		Dialog buyDialog = new Dialog(frame, DIALOG_BUY_STRINGS[DIALOG_FRAME_NAME]){
			ButtonGroup group;
			JCheckBox cbVertical;
			String vertical;
			
			public void actionPerformed(ActionEvent event){
				if(event.getActionCommand().equals(DIALOG_BUY_STRINGS[DIALOG_BUY_PAY])){
					attributes.add(group.getSelection().getActionCommand());
					attributes.add(vertical);
					dispose();
				}
				handleActionEvent(event, attributes);
			}
			
			public void itemStateChanged(ItemEvent e){
				if(e.getStateChange() == ItemEvent.SELECTED){
					vertical = DIALOG_BUY_STRINGS[DIALOG_BUY_VERTICAL];
				} else if(e.getStateChange() == ItemEvent.DESELECTED){
					vertical = "";
				}
			}
			
			public void createDialog(){
				vertical = "";
				getContentPane().setLayout(new FlowLayout());
				JRadioButton []buyItemsRadio = new JRadioButton[STRING_PIECES.length];
				group = new ButtonGroup();
		        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
				for(int i = 0; i < buyItemsRadio.length; i++){
					buyItemsRadio[i] = new JRadioButton(STRING_PIECES[i]);
					buyItemsRadio[i].setActionCommand(STRING_PIECES[i]);
					group.add(buyItemsRadio[i]);
					buyItemsRadio[i].addActionListener(this);
					radioPanel.add(buyItemsRadio[i]);
				}
				JButton bBuy = new JButton(DIALOG_BUY_STRINGS[DIALOG_BUY_PAY]);
				bBuy.addActionListener(this);
				cbVertical = new JCheckBox(DIALOG_BUY_STRINGS[DIALOG_BUY_VERTICAL]);
				cbVertical.addItemListener(this);
				getContentPane().add(radioPanel);
				getContentPane().add(cbVertical);
				getContentPane().add(bBuy);
			}
		};
	}

	public static void showAttackDialog(){
		Dialog attackDialog = new Dialog(frame, DIALOG_ATTACK_STRINGS[DIALOG_FRAME_NAME]){
			public void actionPerformed(ActionEvent event){
			}
			
			public void createDialog(){
				String attackItems[] = { "Jugador 1", "Jugador 2", "Jugador 2", "Jugador 3", "Jugador 4" };
				getContentPane().setLayout(new FlowLayout());
				JRadioButton []buyItemsRadio = new JRadioButton[attackItems.length];
				ButtonGroup group = new ButtonGroup();
		        JPanel radioPanel = new JPanel(new GridLayout(0, 1));
				for(int i = 0; i < buyItemsRadio.length; i++){
					buyItemsRadio[i] = new JRadioButton(attackItems[i]);
					buyItemsRadio[i].setActionCommand(attackItems[i]);
					group.add(buyItemsRadio[i]);
					buyItemsRadio[i].addActionListener(this);
					radioPanel.add(buyItemsRadio[i]);
				}
				JButton bAttack = new JButton(DIALOG_ATTACK_STRINGS[DIALOG_ATTACK_ATTACK]);
				bAttack.addActionListener(this);
		        getContentPane().add(radioPanel);
				getContentPane().add(bAttack);
			}
		};
	}
}
